import operator


# 字典序排列
def nextPermutation(nums):
    l = len(nums)

    # 找到递增子序列
    for i in range(l - 2, -1, -1):
        if nums[i] < nums[i + 1]:
            break
    # 没找到则说明，已是降序排列
    else:
        nums[:] = nums[::-1]
        return

    # 找到比它大的第一个数，并交换
    for j in range(l - 1, -1, -1):
        if nums[j] > nums[i]:
            break

    nums[i], nums[j] = nums[j], nums[i]
    # 对i后面的数进行排序
    nums[i+1:l] = nums[-1:i:-1]


nums = [1, 2, 3, 4, 5]
nextPermutation(nums)
while operator.ne([1, 2, 3, 4, 5], nums):
    print(nums)
    nextPermutation(nums)
